﻿using NewsMine.Utilities;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Net;
using System.IO;
using NewsMine.DomainObjects;
using System.Linq;
using System.Collections.Generic;

namespace NewsMine.Utilities.Tests
{
    
    
    /// <summary>
    ///This is a test class for WebHelperTest and is intended
    ///to contain all WebHelperTest Unit Tests
    ///</summary>
    [TestClass()]
    public class WebHelperTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for DownloadHtmlFromUrl
        ///</summary>
        [TestMethod()]
        public void DownloadHtmlFromUrlTest()
        {
            string url = "http://telugu.webdunia.com/miscellaneous/romance/"; 
            string actual = WebHelper.DownloadHtmlFromUrl(url);
            string actual1 = WebHelper.DownLoadHttpTextByCompressionEnabled(url);

            Assert.AreEqual(actual, actual1);
        }

        [TestMethod()]
        public void DownloadHtmlFromUrlRedirectTest()
        {

            string url = " http://www.sumanasa.com/go/jw03N"; 
            string actual = WebHelper.DownloadHtmlFromUrl(url);
            //string actual1 = WebHelper.DownLoadHttpTextByCompressionEnabled(url);

            //Assert.AreEqual(actual, actual1);
        }

        [TestMethod()]
        public void TestingGoogleShortenigUrl()
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url");
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "POST";

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = "{\"longUrl\":\"http://www.google.com/\"}";
                Console.WriteLine(json);
                streamWriter.Write(json);
            }

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var responseText = streamReader.ReadToEnd();


                Console.WriteLine(responseText);
            }

        }






        /// <summary>
        ///A test for ReadFeed
        ///</summary>
        [TestMethod()]
        public void ReadFeedTest()
        {
            WebHelper.ReadFeed();
            
        }

        /// <summary>
        ///A test for DownLoadHttpTextByCompressionEnabledWithExtraInfo
        ///</summary>
        [TestMethod()]
        public void DownLoadHttpTextByCompressionEnabledWithExtraInfoTest()
        {
            ///string URL = "http://gdata.youtube.com/feeds/base/users/tv9maharashtra/uploads?alt=rss&v=2&orderby=published&client=ytapi-youtube-profile";
            string URL = "http://www.sumanasa.com/go/2sY7j";
            //string URL = "http://feeds.feedburner.com/NDTV-Business";
            //URL = "http://www.thehindu.com/business/?service=rss";

            URL = "https://bobo.codeplex.com/";
            URL = "http://www.andhrajyothy.com/CategoryHome.jsp?category=cinema_news";
            URL = "http://feeds.labnol.org/labnol";
            HtmlContentFromUrl expected = null; 
            HtmlContentFromUrl actual;
            actual = WebHelper.DownLoadHttpTextByCompressionEnabledWithExtraInfo(URL);

            List<HtmlLink> htmlLinks = WebHelper.GetHtmlLinks(actual.FinalUrl, actual);

            var xpaths = (from l in htmlLinks
                          select l.HtmlXpath).ToList<string>().ToArray();
        }

        [TestMethod()]
        public void TestUnWantedChars()
        {
            FeedItem item = new FeedItem();

            Assert.AreEqual(item.PubDate , DateTime.MinValue);

            string testString = "DU����A ��U�z�� �z�ݪ����!";

            char[] chrs = testString.ToCharArray();


        }

        [TestMethod()]
        public void TestJunkUrlStructure()
        {
            string surl = "http://hindi.yahoo.com/%E0%A4%AA%E0%A5%8D%E0%A4%B0%E0%A5%80%E0%A4%A4%E0%A4%BF-%E0%A4%9C%E0%A4%BF%E0%A4%82%E0%A4%9F%E0%A4%BE-%E0%A4%B9%E0%A4%BE%E0%A4%9C%E0%A4%BF%E0%A4%B0-%E0%A4%B9%E0%A5%8B%E0%A4%82-055339509.html;_ylt=AwrwEuFTPDNSSDoA6AEfkYh4;_ylu=X3oDMTQzaHE3NGttBG1pdANDaW5lbWF6emEgVG9wIFN0b3J5BHBrZwM2NGE3YmZmYS1lZDRmLTNmZWItYjAxOS0zZGY1ODc0OGY4ZDIEcG9zAzExBHNlYwN0b3Bfc3RvcnlfY29rZQR2ZXIDOGQwZDAwODEtMWMzOS0xMWUzLWFmOWItMTI4OTE0YTIxNjc2;_ylg=X3oDMTI2MHE5Z3M2BGludGwDaW4EbGFuZwNoaS1pbgRwc3RhaWQDBHBzdGNhdAPgpLjgpL_gpKjgpYfgpK7gpJzgpLzgpL4EcHQDc2VjdGlvbnM-;_ylv=3";

            //Uri u = new Uri(surl);

            surl = "http://localhost/adminsection/searchapi.ashx";

           object obj =  WebHelper.DownLoadHttpDataByCompressionEnabledWithExtraInfo(surl);

        }
    }
}
